#include "StdAfx.h"
#include "SubcableTyingOperatorCmd.h"
#include "SubCableTyingTool.h"
#include <SubCableTyingDisObj.h>


RegisterCommandClass<CSubcableTyingOperatorCmd> RegisterCommandClass(ClassNameToString(CSubcableTyingOperatorCmd));

CSubcableTyingOperatorCmd::CSubcableTyingOperatorCmd(void)
{
	m_pData = NULL;
}


CSubcableTyingOperatorCmd::~CSubcableTyingOperatorCmd(void)
{
	delete  m_pData;
}

bool CSubcableTyingOperatorCmd::Execute()
{
	if (!CMyBaseCommand::Execute())
		return false;

	if (m_pData == NULL)
	{
		if (!GetDataFromXML())
			return false;
	}

	GetHoopsView()->GetSelection()->DeSelectAll();

	HModelLayerModel* pHmodel = (HModelLayerModel *)GetHoopsView()->GetModel();
	CSystem* pBigDrawing = pHmodel->GetBigDrawing();
	CLayer* pActLayer = pBigDrawing->GetActiveLayer();

	switch (m_pData->m_eCmdType)
	{
	case TyingDisObjOperCmdType::TyingDisObj_Create:
		{
			SubCableTyingTool tool(GetHoopsView());
			CSubCableTyingDisObj* pTyingDis = tool.CreateSubCableTying(m_pData->m_lstSubDisObj,m_pData->m_dPosX,m_pData->m_dPosY);
			m_pData->m_lTyingDisObjID = pTyingDis->GetEntityObj()->GetId();
		}
		break;
	case TyingDisObjOperCmdType::TyingDisObj_Delete:
		{
			SubCableTyingTool tool(GetHoopsView());
			CSubCableTyingDisObj* pTyingDisobj = pActLayer->FindTyingDisObjById(m_pData->m_lTyingDisObjID);
			if (pTyingDisobj)
			{
				m_pData->m_dPosX = pTyingDisobj->GetPosX();
				m_pData->m_dPosY = pTyingDisobj->GetPosY();
				m_pData->m_lstSubDisObj = tool.DeleteSubCableTying(pTyingDisobj);
			}
		}
		break;
	case TyingDisObjOperCmdType::TyingDisObj_Move:
		{
			SubCableTyingTool tool(GetHoopsView());
			CSubCableTyingDisObj* pTyingDisobj = pActLayer->FindTyingDisObjById(m_pData->m_lTyingDisObjID);
			if (pTyingDisobj)
			{
				tool.DealMoveAndAssoSubCableDisPts(pTyingDisobj,m_pData->m_dNewPosX,m_pData->m_dNewPosY);
			}
		}
		break;
	default:
		break;
	}

	pHmodel->Update();

	return true;
}

bool CSubcableTyingOperatorCmd::Unexecute()
{
	if (!CMyBaseCommand::Unexecute())
		return false;

	GetHoopsView()->GetSelection()->DeSelectAll();

	if (m_pData == NULL)
	{
		if (!GetDataFromXML())
			return false;
	}

	HModelLayerModel* pHmodel = (HModelLayerModel *)GetHoopsView()->GetModel();
	CSystem* pBigDrawing = pHmodel->GetBigDrawing();
	CLayer* pActLayer = pBigDrawing->GetActiveLayer();
	switch (m_pData->m_eCmdType)
	{
	case TyingDisObjOperCmdType::TyingDisObj_Create:
		{
			CSubCableTyingDisObj* pTyingDis = pActLayer->FindTyingDisObjById(m_pData->m_lTyingDisObjID);
			SubCableTyingTool tool(GetHoopsView());
			tool.DeleteSubCableTying(pTyingDis);
		}
		break;
	case TyingDisObjOperCmdType::TyingDisObj_Delete:
		{
			SubCableTyingTool tool(GetHoopsView());
			tool.CreateSubCableTying(m_pData->m_lstSubDisObj,m_pData->m_dPosX,m_pData->m_dPosY);
		}
		break;
	case TyingDisObjOperCmdType::TyingDisObj_Move:
		{
			SubCableTyingTool tool(GetHoopsView());
			CSubCableTyingDisObj* pTyingDisobj = pActLayer->FindTyingDisObjById(m_pData->m_lTyingDisObjID);
			if (pTyingDisobj)
			{
				tool.DealMoveAndAssoSubCableDisPts(pTyingDisobj,m_pData->m_dPosX,m_pData->m_dPosY);
			}
		}
		break;
	default:
		break;
	}

	pHmodel->Update();

	return true;
}

bool CSubcableTyingOperatorCmd::SaveDataToXML()
{
	Begin_SaveDataToXML
	{
		/*TiXmlElement itemMergeBusData("Merge_bus_to_bus_data");
		vector<long> lstWires = m_pData->m_vecSchematicBusID;
		if (lstWires.empty())
		{
		return false;
		}
		for (vector<long>::iterator it=lstWires.begin(); it!=lstWires.end(); it++)
		{
		TiXmlElement itemWire("SchBus");
		itemWire.SetAttribute("Id", *it);
		itemMergeBusData.InsertEndChild(itemWire);
		}
		itemCmd.InsertEndChild(itemMergeBusData);*/
	}
	End_SaveDataToXML
	return true;
}

bool CSubcableTyingOperatorCmd::GetDataFromXML()
{
	if (m_pData)
		return true;
	else
		m_pData = new TyingDisObjOperData;

	CSystem* pSystem = CModelObjOption::GetIModelObjEventHandler()->GetActiveSystem();
	Begin_GetDataFromXML
	{
	/*	vector<long> lstWiresForMerge;
		TiXmlElement* elemMergeBusData = elemCmd->FirstChildElement("Merge_bus_to_bus_data");
		if (elemMergeBusData)
		{
			for (TiXmlElement* itemWire=elemMergeBusData->FirstChildElement("SchBus");
				itemWire; itemWire=itemWire->NextSiblingElement("SchBus"))
			{
				int nId = 0;
				itemWire->Attribute("Id", &nId);
				lstWiresForMerge.push_back(nId);
			}
		}
		m_pData->m_vecSchematicBusID = lstWiresForMerge;*/
	}
	End_GetDataFromXML

	return true;
}

bool CSubcableTyingOperatorCmd::DeleteData()
{
	delete m_pData;
	m_pData = NULL;
	return true;
}

void CSubcableTyingOperatorCmd::PrepareData( TyingDisObjOperData* pData )
{
	m_pData = pData;
	
	End_PrepareData	
}

void CSubcableTyingOperatorCmd::CallCmd()
{
	MY_CALLCOMMAND(this);
}
